package com.changgou.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    // @Value("${pagePath}")
    private String pagePath = "/Users/humac/code-java/javaee8_web_project_2/changgou-parent/changgou-web/changgou-web-item/src/main/resources/templates/items";

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void createHtml(Long spuId) {
        // 上下文
        Context context = new Context();
        Map<String, Object> dataMap = buildDataModel(spuId);
        context.setVariables(dataMap);
        File file = new File(pagePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File dest = new File(file, spuId + ".html");

        // 生成页面
        try(PrintWriter writer = new PrintWriter(dest, "utf-8")) {
            templateEngine.process("item", context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Map<String, Object> buildDataModel(Long spuId) {
        // 构建数据
        Map<String, Object> dataMap = new HashMap<>();
        // 获取spu和sku列表数据
        Result<Spu> spuResult = spuFeign.findById(spuId);
        Spu spu = spuResult.getData();

        // 三级分类信息
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        dataMap.put("category1", category1);
        dataMap.put("category2", category2);
        dataMap.put("category3", category3);

        // 图片信息
        if (spu.getImages() != null) {
            dataMap.put("imageList", spu.getImages().split(","));
        }
        // 规格信息
        Map specItemsMap = JSON.parseObject(spu.getSpecItems(), Map.class);
        dataMap.put("specList", specItemsMap);

        dataMap.put("spu", spu);

        // 查询sku集合的详细信息
        Sku condition = new Sku();
        condition.setSpuId(spuId);
        Result<List<Sku>> skuList = skuFeign.findList(condition);

        dataMap.put("skuList", skuList.getData());
        return dataMap;
    }
}
